<template>
  <div>
    <el-card shadow="never" class="shadow">
      <div class="el-card__header" v-if="$route.query.id">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item>学科管理</el-breadcrumb-item>
          <el-breadcrumb-item v-if="$route.query.name">{{$route.query.name}}</el-breadcrumb-item>
          <el-breadcrumb-item>目录管理</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <!-- 搜索nav栏 -->
      <div class="box">
        <div class="box_left">
          <el-form :inline="true" :model="formInline">
            <el-form-item label="目录名称">
              <!-- 注释 -->
              <el-input v-model="formInline.directoryName"></el-input>
            </el-form-item>
            <el-form-item label="状态" class="zhuangtai">
              <el-select v-model="formInline.state" placeholder="请选择">
                <el-option label="启用" value="1"></el-option>
                <el-option label="禁用" value="0"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button size="small" @click="resetInput">清除</el-button>
              <el-button
                size="small"
                class="colInput"
                type="primary"
                @click="search()"
                >搜索</el-button
              >
            </el-form-item>
          </el-form>
        </div>
        <div class="box_right">
          <el-button  type="text" icon="el-icon-back" @click="getSubjects" v-if="$route.query.id"
            >返回学科</el-button
          >
          <!-- 把新增标签改成了新增目录 -->
          <el-button type="success" icon="el-icon-edit" @click="addDirectirys"
            >新增目录</el-button
          >
        </div>
      </div>

      <!-- 消息提示的文案 -->
      <el-alert
        :title="`数据一共 ${counts} 条`"
        :closable="false"
        type="info"
        show-icon
      ></el-alert>
      <!--表格区域 -->
      <el-table class="form" :data="directoryslist" width:=100% v-loading="isLoading" >
        <el-table-column type="index" label="序号" :index="indexMethod" width="80"></el-table-column>
        <el-table-column prop="subjectName" label="所属学科" style="width: 10%"></el-table-column>
        <el-table-column
          prop="directoryName"
          label="目录名称"
           style="width: 10%"
        ></el-table-column>
        <el-table-column
          prop="username"
          label="创建者"
           style="width: 10%"
        ></el-table-column>
        <el-table-column prop="addDate" label="创建日期" width="180">
          <template #default="{ row }">
            {{ formaTime(row.addDate) }}
          </template>
        </el-table-column>
        <el-table-column prop="totals" label="面试题数量" style="width: 10%"></el-table-column>
        <el-table-column
          prop="state"
          label="状态"
          style="width: 10%"
          :formatter="formateDate"
        >
        </el-table-column>
        <el-table-column prop="address" label="操作" style="width: 10%">
          <template #default="{ row }">
            <el-button type="text" size="small" @click="changeState(row)">{{row.state === 1 ? '禁用' : '启用'}}</el-button>
            <el-button type="text" size="small" @click="editDirectory(row)" :disabled="row.state == 0 ? false : true">修改</el-button>
            <el-button type="text" size="small" @click="delDirectory(row.id)" :disabled="row.state == 0 ? false : true">删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页组件 :current-page:当前页面 -->
      <div style="height: 60px; margin-right: 10px">
        <el-pagination
          class="fenye"
          background
          :current-page="queryList.page"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="queryList.pagesize"
          layout="total,sizes,prev, pager, next, jumper"
          :total="counts"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :hide-on-single-page="fenyehide"
        >
        </el-pagination>
      </div>
    </el-card>
    <!-- 新增组件 -->
    <directirysAdd :show-dialog.sync="showDialog" :data="editItem" :adds='adds'></directirysAdd>
  </div>
</template>

<script>
import directirysAdd from '../components/directorys-add.vue'
import { list, remove, changeState } from '../../api/hmmm/directorys'
import dayjs from 'dayjs'

export default {
  components: {
    directirysAdd
  },
  data () {
    return {
      adds: false,
      counts: 0,
      directoryslist: [],
      // 清空数据
      formInline: {
        directoryName: '',
        state: ''
      },
      // 请求目录数据参数
      queryList: {
        page: 1,
        pagesize: 10
      },
      // loading
      isLoading: false,
      // 弹框
      showDialog: false,
      editItem: null,
      fenyehide: false
    }
  },
  // 调用
  created () {
    // 这个三元意思 判断 如果不是从学科页面跳转过来的 就使用pagesgoto() 则调用原始方法
    // Object.keys(this.$route.query).length > 0 ? this.pagesgoto() : this.directorysList()
    this.$route.query ? this.pagesgoto() : this.directorysList()
  },
  methods: {
    search () {
      this.$route.query ? this.pagesgoto() : this.directorysList()
    },
    // 跳转页面
    async pagesgoto () {
      this.isloading = true
      const { directoryName, state } = this.formInline
      const queryList = { ...this.queryList }
      if (directoryName) {
        queryList.directoryName = directoryName
      }
      if (state !== '') {
        queryList.state = state
      }
      // this.fenyehide = false
      const { data } = await list({
        ...this.queryList,
        subjectID: this.$route.query.id,
        directoryName: queryList.directoryName,
        state: queryList.state
      })
      this.directoryslist = data.items
      this.counts = data.counts
      this.fenyehide = this.counts <= queryList.pagesize
      // console.log(this.fenyehide)
      this.isLoading = false
    },
    // 删除
    delDirectory (id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await remove(id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      }).finally(() => {
        this.$route.query ? this.pagesgoto() : this.directorysList()
      })
    },
    // 修改
    async editDirectory (data) {
      this.editItem = { ...data }
      this.adds = false
      this.showDialog = true
    },
    // 启用禁用
    async changeState (data) {
      const { id, state } = data
      await changeState({ id, state: state === 1 ? 0 : 1 })
      this.$message({
        message: '操作成功',
        type: 'success'
      })
      await this.$route.query ? this.pagesgoto() : this.directorysList()
    },
    // 点击显示添加弹框
    addDirectirys () {
      this.editItem = {
        subjectName: Object.keys(this.$route.query).length > 0 ? this.$route.query.names : '',
        directoryName: ''
      }
      this.adds = true
      this.showDialog = true
    },
    // 清空表单
    resetInput () {
      this.formInline.directoryName = ''
      this.formInline.state = ''
      this.$route.query ? this.pagesgoto() : this.directorysList()
    },
    // 获取列表数据
    async directorysList () {
      this.isLoading = true
      const { directoryName, state } = this.formInline
      const queryList = { ...this.queryList }
      if (directoryName) {
        queryList.directoryName = directoryName
      }
      if (state !== '') {
        queryList.state = state
      }
      const { data } = await list(queryList)
      this.directoryslist = data.items
      this.counts = data.counts
      this.isLoading = false
    },
    // 日期修改
    formaTime (time) {
      return dayjs(time).format('YYYY-MM-DD HH:mm:ss')
    },
    // 状态的改变
    formateDate (row, column, cellValue, index) {
      // 启用为1 禁用为0
      if (cellValue === 1) {
        return '已启用'
      }
      if (cellValue === 0) {
        return '已禁用'
      }
    },
    // 序号，计算页容量
    indexMethod (index) {
      return (this.queryList.page - 1) * this.queryList.pagesize + index + 1
    },
    // 页容量改变,一页多少条
    handleSizeChange (newSize) {
      this.queryList.pagesize = newSize
      this.$route.query ? this.pagesgoto() : this.directorysList()
    },
    // 页码改变, 多少页
    handleCurrentChange (newPage) {
      this.queryList.page = newPage
      this.$route.query ? this.pagesgoto() : this.directorysList()
    },
    // 页面跳转
    getSubjects () {
      this.$router.push('/subjects/list')
    }
  }
}
</script>

<style scoped lang="scss">
.shadow {
  margin: 10px;
}
.el-card__header {
  padding: 0 0 18px 0;
  border-bottom: 1px solid #ebeef5;
  // -webkit-box-sizing: border-box;
  box-sizing: border-box;
}
.box {
  display: flex;
  padding-top: 18px;
  justify-content: space-between;
}
.zhuangtai {
  padding-left: 50px;
}
.fenye {
  float: right;
}
.form {
  margin-bottom: 50px;
}
</style>
